package com.sunspring.zdcall.controller.wbcontroller;

import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.service.QueueService;
import com.sunspring.zdcall.support.cache.QueueCache;
import com.sunspring.zdcall.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 诊室医生操作控制层
 * @Author WuLei
 * @Date 2018/11/29  10:39
 */
@RestController
@Slf4j
public class VisitController {

    @Autowired
    private QueueCache queueCache;

    @Autowired
    private SimpMessagingTemplate smt;

    @Autowired
    private QueueService queueService;

    /**
     * 医生呼号
     * @param deptId 科室Id
     * @param checkRoomId 患者所处检查室Id
     * @param checkRoomName 当前呼号端所处检查室名称
     * @param queueId 排队号
     */
    @GetMapping("/zd-server/call")
    public void call(String deptId,String checkRoomId,String checkRoomName,String queueId,String patientName) {
        log.info("有新的呼号,检查室Id=" + checkRoomId + ",排队号=" + queueId);

        Map<String,String> map = new HashMap<>(3);
        map.put("checkRoom",checkRoomName);
        map.put("queueId",queueId);
        map.put("patientName",patientName);
        map.put("checkRoomId",checkRoomId);
        String json = JsonUtil.write(map);

        //通知大屏 准备呼号
        smt.convertAndSend("/call/dept/" + deptId, json);

        //通知小屏 准备呼号
        smt.convertAndSend("/call/checkRoom/" + checkRoomId,json);
    }


    /**
     * 医生过号
     * @param deptId 科室Id
     * @param checkRoomId 检查室Id
     * @param queueId 排队号
     */
    @GetMapping("/zd-server/pass")
    public void pass(String deptId,String checkRoomId,String queueId,String inspectId) {

        //修改队列等待人数
        queueCache.pass(deptId,checkRoomId,queueId);

        //修改数据库状态
        queueService.changeCheckStatus(inspectId,1);

        //通知大屏，过号
        smt.convertAndSend("/pass/dept/" + deptId,queueId);

        //通知小屏，过号
        smt.convertAndSend("/pass/checkRoom/" + checkRoomId,queueId);
    }


    /**
     * 就诊完毕
     * @param queueId 队列号
     */
    @GetMapping("/zd-server/finish")
    public void visitFinish(String deptId,String checkRoomId,String queueId,String inspectId) {

        //修改队列等待人数
        queueCache.finish(deptId,checkRoomId,queueId);

        //修改数据库
        queueService.finishVisit(inspectId,new Date());

        //通知大屏
        smt.convertAndSend("/finish/dept/" + deptId,queueId);

        //通知小屏
        smt.convertAndSend("/finish/checkRoom/" + checkRoomId,queueId);

    }

    /**
     * 打印完成
     * @param deptId
     * @param checkRoomId
     * @param queueId
     */
    @PostMapping("/zd-server/printFinish")
    public SimpleResponse finishPrint(String deptId,String checkRoomId,String queueId,String inspectId) {
        //修改队列
        queueCache.finishPrint(deptId,checkRoomId,queueId,1);

        //修改数据库
        queueService.updatePrintStatus(inspectId,1);
        return SimpleResponse.success();
    }

    /**
     * 推送给客户端患者信息
     */
    private static class CallInfo {
        String checkRoomName;

        String patientName;

        String queueId;

        public CallInfo(String checkRoomName, String patientName, String queueId) {
            this.checkRoomName = checkRoomName;
            this.patientName = patientName;
            this.queueId = queueId;
        }
    }


}
